package com.jiangjiang.eshop.inventory.updater;

import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.inventory.domain.GoodsStockEntity;
import com.jiangjiang.eshop.inventory.service.InventoryGoodsStockService;
import com.jiangjiang.eshop.wms.dto.PurchaseInputOrderDTO;
import com.jiangjiang.eshop.wms.dto.PurchaseInputOrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author Jianghaowen
 * @Date 2021/6/7
 * @Description: TODO
 * @Version V1.0
 **/
@Service
public class PurchaseInputStockUpdaterFactory<T> extends  AbstractStockUpdaterFactory<T>{


    /**
     * 构造方法
     *
     * @param goodsStockService 商品库存service 组件
     * @param dateHelper 时间管理组件
     */
    @Autowired
    public PurchaseInputStockUpdaterFactory(
            InventoryGoodsStockService goodsStockService,
            DateHelper dateHelper) {
        super(goodsStockService, dateHelper);
    }

    @Override
    protected List<Long> getGoodsSkuIds(T parameter) {
        PurchaseInputOrderDTO dto = (PurchaseInputOrderDTO) parameter;
        return dto.getItems().stream().map(PurchaseInputOrderItemDTO::getGoodsSkuId).collect(Collectors.toList());
    }

    @Override
    protected StockUpdater createUpdater(List<GoodsStockEntity> goodsStock, T parameter) {
        PurchaseInputOrderDTO dto = (PurchaseInputOrderDTO) parameter;
        Map<Long, PurchaseInputOrderItemDTO> itemDTOMap = dto.getItems()
                .stream()
                .collect(Collectors.toMap(PurchaseInputOrderItemDTO::getGoodsSkuId, Function.identity()));
        return new PurchaseInputStockUpdater(goodsStockService, goodsStock, dateHelper, itemDTOMap);
    }
}
